package com.lh.pospi.x_store.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.flyco.tablayout.SlidingTabLayout;
import com.lh.pospi.x_store.R;
import com.lh.pospi.x_store.app.MyApp;
import com.lh.pospi.x_store.base.BaseActivity;
import com.lh.pospi.x_store.ui.fragment.BsFragment;
import com.lh.pospi.x_store.ui.fragment.KcFragment;
import com.lh.pospi.x_store.ui.fragment.PdFragment;
import com.lh.pospi.x_store.ui.fragment.PfFragment;
import com.lh.pospi.x_store.ui.fragment.PurchaseFragment;
import com.lh.pospi.x_store.ui.purchase.PurchaseActivity;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
//    @BindView(R.id.stl)
//    SlidingTabLayout mStl;
    @BindView(R.id.contentViewPager)
    ViewPager mViewPager;
    private final String[] mTitles = {"采购", "盘点", "批发", "损溢", "库存"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter mAdapter;
    private long mBackPressed;
    private static final int TIME_INTERVAL = 2000;


    @Override
    protected int getContentView() {
        return R.layout.activity_tab;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        getTopBar().setTitle(MyApp.getInstance().getmUserBean().getName());
        getTopBar().removeAllLeftViews();
        Button upButton = getTopBar().addRightTextButton("退出", R.mipmap.icon_up);
        upButton.setTextColor(getResources().getColor(R.color.white));
        upButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final RxDialogSureCancel dialogSureCancel = new RxDialogSureCancel(mContext);
                dialogSureCancel.setTitle("提示");
                dialogSureCancel.setContent("是否退出登录");
                dialogSureCancel.setSureListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialogSureCancel.dismiss();
                        lunchActivity(LoginActivity.class);
                        finish();
                    }
                });
                dialogSureCancel.setCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialogSureCancel.dismiss();
                    }
                });
                dialogSureCancel.show();

            }
        });
//        getTopBar().addLeftTextButton(MyApp.getInstance().getmUserBean().getName(), 0).setTextColor(getResources().getColor(R.color.white));
        initView();
//        checkVertion();
    }

    private void initView() {
        mFragments.add(PurchaseFragment.getInstance());
//        mFragments.add(PdFragment.getInstance());
//        mFragments.add(PfFragment.getInstance());
//        mFragments.add(BsFragment.getInstance());
//        mFragments.add(KcFragment.getInstance());
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
//        mStl.setViewPager(mViewPager);
    }

//    @OnClick({R.id.ll_purchase})
//    public void onClick(View v){
//        switch (v.getId()) {
//            case R.id.ll_purchase:
//                lunchActivity(PurchaseActivity.class);
//                break;
//        }
//    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
    @Override
    public void onBackPressed() {
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            super.onBackPressed();
            return;
        } else {
            Toast.makeText(getBaseContext(), "再次点击返回键退出", Toast.LENGTH_SHORT).show();
        }
        mBackPressed = System.currentTimeMillis();
    }
}
